import cTitle from "components/title";
import dayjs from "dayjs";

export default {
  components: { cTitle },
  data() {
    return {
      dateFormat: 'YYYY-MM-DD',
      userInfo: {},
      identity: [],
      qrShow: false,
      partner: {
        totality: '-',
        push_num: '-',
        merchantCount: '-',
        dividend_amount: '-'
      },
      calendarVisible: false,
      minDate: new Date(2024, 1, 1),
      maxDate: new Date(),
      defaultDate: null,
      dateRangeValue: '',
      startStr: '',
      endStr: '',
      tabType: 1,
      page: 1,
      pageSize: 10,
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      isSkeleton: true,
      imageUrl: ''
    };
  },

  mounted() {
    this.initDate();
  },

  methods: {
    /**
     * 初始化日期
     */
    initDate: function() {
      let firstDay = new Date();
      firstDay.setDate(1);
      this.defaultDate = [firstDay, new Date()];
      this.startStr = dayjs().date(1).format(this.dateFormat);
      this.endStr = dayjs().format(this.dateFormat);
    },

    /**
     * 刷新数据
     */
    refreshData: function() {
      this.finished = false;
      this.loading = true;
      this.refreshing = true;
      this.isSkeleton = true;
      this.list = [];
      this.page = 1;
      this.getData();
    },

    /**
     * 获取数据
     */
    getData: function() {
      const { startStr, endStr, tabType, page, pageSize } = this;
      const json = {
        start_time: startStr,
        end_time: endStr,
        page,
        pageSize,
        tab: tabType
      };
      const that = this;
      $http.get("plugin.team-partner.api.team-partner.getPartnerInfo", json)
        .then(response => {
          const { result, data } = response;
          if (result == 1) {
            const { partner, member, merchantList } = data;
            // 数据
            that.partner = partner;
            // 会员信息
            that.userInfo = member;
            if (member.identity && member.identity.length) {
              // 身份信息
              that.identity.splice(0, that.identity.length, ...member.identity);
            }
            that.list = that.list.concat(merchantList.data);
            if (that.list.length >= merchantList.total) {
              that.finished = true;
            } else {
              that.page++;
            }
          }
        })
        .finally(() => {
          that.loading = false;
          that.refreshing = false;
          that.isSkeleton = false;
        });
    },

    /**
     * 日期选中
     * @param {Array} date 选中的日期
     */
    calendarConfirm: function(date) {
      this.startStr = dayjs(date[0]).format(this.dateFormat);
      this.endStr = dayjs(date[1]).format(this.dateFormat);
      this.calendarVisible = false;
      this.refreshData();
    },

    /**
     * 选项卡切换
     * @param {Number | String} type 类型
     */
    tabChange: function(type) {
      this.tabType = type;
      this.refreshData();
    },

    /**
     * 获取二维码
     */
    getPoster: function() {
      const that = this;
      const json = {
        partner: 1
      };
      $http.get("plugin.team-partner.api.team-partner.getPoster", json)
        .then(response => {
          if (response.result == 1) {
            that.imageUrl = response.data.image_url;
            that.qrShow = true;
          } else {
            that.$dialog.alert({ message: response.msg });
          }
        });
    }
  }
};
